const api = require('../../config/api.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        showProp: false,
        value: '',
        imgUrls: [],
        hidden: true,
        scrollTop: 0,
        scrollHeight: 0,
        classify: 0,
        key: '',
        pageNum: 1,
        pageSize: 30,
        over: false,
        top: ''
    },
    onChange(e) {
        this.setData({
            key: e.detail
        })
    },
    onSearch() {
        this.setData({
            pageNum: 1,
            imgUrls: []
        })
        if (this.data.key) {
            this.loadMore();
        } else {
            wx.showToast({
                title: '请输入查询内容',
                duration: 2000,
                icon: 'error'
            });
        }
    },
    valided() {
        this.setData({
            showProp: false
        })
        let hasUser = getApp().hasUser()
        if (!hasUser) {
            this.setData({
                showProp: true
            })
            return false
        } else {
            this.setData({
                showProp: false
            })
            return true
        }
    },
    onClick() {
        this.setData({
            pageNum: 1,
            imgUrls: []
        })
        if (this.data.key) {
            this.loadMore();
        } else {
            wx.showToast({
                title: '请输入查询内容',
                duration: 2000,
                icon: 'error'
            });
        }
    },
    goDetail(e) {
        if (!this.valided()) {
            return
        }
        const { tag } = e.currentTarget.dataset
        let data = this.data.imgUrls.find(x => x.id == tag)
        if (data) {
            let dataStr = JSON.stringify(data)
            wx.navigateTo({
                url: `/pages/priceDetail/index?dataStr=${dataStr}`
            });
        }

    },

    loadMore() {
        this.setData({
            hidden: false
        });
        wx.request({
            url: api.priceListUrl,
            data: {
                pageNum: this.data.pageNum,
                pageSize: this.data.pageSize,
                materialName: this.data.key
            },
            success: (res) => {
                if (res.data.code === 200) {
                    var list = this.data.imgUrls;
                    if (res.data.search.records.length != 0) {
                        for (var i = 0; i < res.data.search.records.length; i++) {
                            list.push(res.data.search.records[i]);
                        }
                        this.setData({
                            imgUrls: list,
                            hidden: true,
                            pageNum: this.data.pageNum + 1
                        });
                    } else {
                        this.setData({
                            imgUrls: list,
                            hidden: true,
                            pageNum: this.data.pageNum,
                            over: true
                        });
                    }

                }


            }
        });
    },
    //页面滑动到底部
    bindDownLoad: function () {
        var that = this;
        if (this.data.over == false) {
            that.loadMore();
        } else {
            wx.showToast({
                title: '客官，没有啦',
                icon: 'none'
            });
            return
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const { classify } = options

        if (classify == 1) {
            this.setData({
                classify: 1
            })
        } else {
            this.setData({
                classify: 0
            })
        }
        var that = this;
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    scrollHeight: res.windowHeight
                });
            }
        });

        let query = wx.createSelectorQuery();
        query.select('.searchDetail').boundingClientRect(rect => {
            //获取到元素
            let height = rect.height;
            //给页面赋值
            this.setData({
                top: height + "px"
            }, function () {

            })
        }).exec();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        // this.setData({
        //   pageNum:1
        // })

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        // this.setData({
        //   pageNum:1
        // })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})